﻿/*-----------------------------------------------
// File: BCAttackMoveBackState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class BCAttackMoveBackState : Fsm.FsmStateMonoBase<ActorState>
    {
        BattleCharacter bc
        {
            get { return this.controller as BattleCharacter; }
        }
        public BCAttackMoveBackState(ActorState state, BattleCharacter ctrl) : base(state, ctrl) { }

        Vector3 targetPos;
        float deltaTime = 0f;

        float moveBackTime
        {
            get { return bc.GetAniLength(AniName.JUMP_BACKWARD); }
        }
        float moveSpeed;
        Vector3 moveDir;
        public override void Enter()
        {
            deltaTime = 0;
            targetPos = bc.GetStandPosition();
            var dis = Vector3.Distance(bc.transform.localPosition, targetPos);
            if (dis > 0)
            {
                moveSpeed = Vector3.Distance(bc.transform.localPosition, targetPos) / ((float)bc.jumpBackFallTime - (float)bc.jumpBackFlyTime);
                moveDir = (targetPos - bc.transform.localPosition).normalized;
                bc.PlayJumpBackward();
            }
            else
            {
                moveSpeed = 0;
                moveDir = Vector3.zero;
                bc.PlayStand();
            }
        }
        public override void Update()
        {
            deltaTime += Time.deltaTime;
            if (deltaTime >= bc.jumpBackFlyTime && deltaTime <= bc.jumpBackFallTime)
            {
                var newPos = bc.transform.localPosition + moveDir * moveSpeed * Time.deltaTime;
                bc.transform.localPosition = newPos;
            }
            //var dis = Vector3.Distance(bc.transform.localPosition, targetPos);
            if (deltaTime >= moveBackTime - 0.1f)
            {
                //bc.anim.Play(AniName.IDLE_FIGHT, WrapMode.Loop);
                bc.SwitchState(ActorState.AttackReady);
            }

            if (bc.IsHavePackageNeedDo()) {
                var package = bc.DequeuePackage();
                bc.DoPackage(package);
            }
        }
    }
}
